<?php

declare(strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use \think\facade\Db;
use think\facade\Cache;

// const PWD = md5(sha1("123456") . 'be9b08d2-5e6b-73c4-6e2b-8767fbd3f6c9');
class genuser extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\genuser')
            ->setDescription('the app\command\genuser command');
    }

    protected function execute(Input $input, Output $output)
    {
        $GLOBALS['pwd'] = md5(sha1("123456") . 'be9b08d2-5e6b-73c4-6e2b-8767fbd3f6c9');
        $GLOBALS['sql'] = "insert into admin_user (username,nickname,password,status) values ";
        Db::table('admin_budget')->whereBetween('id','1, 13197')->chunk(1000, function ($rows) {
            
            // var_dump($rows);
            foreach ($rows as $row) {
                
                $username = $row['district_code'] . $row['institutions_code'];

                $key = 'key_' . $username;
                $is = Cache::get($key);
                if ($is === NULL) {
                    $GLOBALS['sql'] .= "(";
                    echo $username . "\n";
                    $password = $GLOBALS['pwd'];
                    $GLOBALS['sql'] .= "'" . $username . "','" . $row['institutions_name'] . "','" . $password . "',";
                    Cache::store('redis')->set($key, '1', 7200);
                    $GLOBALS['sql'] .= "'1'),";
                }
            }
        });
        $s2 = substr($GLOBALS['sql'], 0, -1);
        // $s2 = ;
        echo $s2 . "\n";
        Db::query($s2);

        exit;
        // 指令输出
        // $output->writeln('app\command\genuser');
    }
}
